package com.jl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.commons.utils.BASE64;
import com.jl.model.DataFile;
import com.jl.service.IDataFileService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author JiangJian
 */
@Controller
@RequestMapping("/dataFile")
public class DataFileController extends BaseController {

    @Autowired
    public IDataFileService dataFileService;


    @GetMapping("dataFileDataGrid")
    @RequiresPermissions("/dataFile/dataFileDataGrid")
    public String dataFilePage(ModelMap modelMap, Long id){
        return "dataFile_list";
    }


    @GetMapping("add")
    @RequiresPermissions("/dataFile/add")
    public String addDataFilePage(ModelMap modelMap, Long id){
        modelMap.put("id",id);
        return "dataFile_add";
    }


    @PostMapping("/add")
    @RequiresPermissions("/dataFile/add")
    @ResponseBody
    public ResultData addDevice(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        DataFile dataFile = JSON.parseObject(value,DataFile.class);
        //新增数据
        dataFile.setCreateId(getUserId());
        dataFile.setGmtCreate(new Date());

        dataFileService.save(dataFile);
        return success(dataFile);
    }

    @GetMapping("edit")
    @RequiresPermissions("/dataFile/edit")
    public String editDataFilePage(ModelMap modelMap, Long id){
        modelMap.put("id",id);
        return "dataFile_add";
    }


    @PostMapping("/edit")
    @RequiresPermissions("/dataFile/edit")
    @ResponseBody
    public ResultData editDevice(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        DataFile dataFile = JSON.parseObject(value,DataFile.class);
        //id存在表示是更新数据
        dataFile.setGmtModified(new Date());
        dataFile.setModifyId(getUserId());

        dataFileService.updateById(dataFile);
        return success(dataFile);
    }


    @PostMapping("/delete")
    @RequiresPermissions("/dataFile/delete")
    @ResponseBody
    public ResultData deletesDevice(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
        boolean b = dataFileService.removeByIds(ids);
        if(b){
            return success();
        }
        return fail();
    }


    @PostMapping("/dataGrid")
    @RequiresPermissions("/dataFile/dataGrid")
    @ResponseBody
    public ResultData deviceDataGrid(Long current, Long pageSize, String searchCol, String searchValue,String json){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<DataFile> queryWrapper = new QueryWrapper<>();
        //目前的查询只有单表
        if(StringUtils.isNotBlank(json)){
            String value = BASE64.decode(json);
            JSONObject jsonObject = JSONObject.parseObject(value);
            if(StringUtils.isNotBlank(jsonObject.getString("name"))){
                queryWrapper.like("t.name",jsonObject.getString("name"));
            }
            if(null!=jsonObject.getLong("deviceId") && jsonObject.getLong("deviceId")!=0){
                queryWrapper.eq("t.device_id",jsonObject.getString("deviceId"));
            }
            if(null!=jsonObject.getInteger("label") && jsonObject.getInteger("label")!=0){
                queryWrapper.eq("t.label",jsonObject.getString("label"));
            }
        }else{
            if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
                if(searchCol.equals(Constants.ID)){
                    queryWrapper.eq("t.id",searchValue);
                }else{
                    queryWrapper.like("t."+searchCol,searchValue);
                }
            }
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        dataFileService.selectDataFileDataGrid(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }

}